package cn.plushine.controller;

import cn.plushine.pojo.Brand;
import cn.plushine.pojo.Warehouse;
import cn.plushine.pojo.WarehouseArea;
import cn.plushine.pojo.WarehousePosition;
import cn.plushine.service.BaseBrandService;
import cn.plushine.service.BaseWarehouseService;
import cn.plushine.utils.ResMapUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 仓库信息
 */
@Api(tags = "仓库信息")
@RestController
@RequestMapping("/admin/product/warehouse/")
public class BaseWarehouseController {
    @Autowired
    BaseWarehouseService baseWarehouseService;

    // 获取仓库信息
    @ApiOperation(value = "获取仓库信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "input", value = "关键字", paramType = "query", dataType = "String")
//    })
    @GetMapping("/get")
    public HashMap<String, Object> get() {
        try {
            // 获取仓库信息
            List<Warehouse> warehouseListDB = baseWarehouseService.get();
            // 成功响应
            return ResMapUtils.encapsulation(200, "获取仓库信息成功！", warehouseListDB);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "获取仓库信息失败！", e.getMessage());
        }
    }

    // 获取指定仓库区、位信息
    @ApiOperation(value = "获取指定仓库区、位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "仓库id", paramType = "path", dataType = "int", example = "1")
    })
    @GetMapping("/getDetail/{id}")
    public HashMap<String, Object> getDetail(@PathVariable int id) {
        try {
            // 获取指定仓库区、位信息
            List<HashMap> warehouseListDB = baseWarehouseService.getDetail(id);
            // 成功响应
            return ResMapUtils.encapsulation(200, "获取指定仓库区、位信息成功！", warehouseListDB);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "获取指定仓库区、位信息失败！", e.getMessage());
        }
    }

    // 更新仓库信息
    @ApiOperation(value = "更新仓库信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warehouse", value = "仓库信息", paramType = "body", dataType = "Warehouse")
    })
    @PostMapping("/modify")
    public HashMap<String, Object> modify(@RequestBody Warehouse warehouse) {
        try {
            // 更新仓库信息
            baseWarehouseService.modify(warehouse);
            // 删除指定仓库图片
            baseWarehouseService.deleteImage(warehouse.getId());
            // 插入图片
            baseWarehouseService.addImage(warehouse.getId(), warehouse.getImage());
            // 成功响应
            return ResMapUtils.encapsulation(200, "更新仓库信息成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "更新仓库信息失败！", e.getMessage());
        }
    }

    // 添加仓库信息
    @ApiOperation(value = "添加仓库信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warehouse", value = "仓库信息", paramType = "body", dataType = "Warehouse")
    })
    @PostMapping("/add")
    public HashMap<String, Object> add(@RequestBody Warehouse warehouse) {
        try {
            // 查询最后一条数据的id
            int id = baseWarehouseService.getFinally().getId() + 1;
            // 给新添加的仓库信息的id赋值
            warehouse.setId(id);
            // 添加仓库信息
            baseWarehouseService.add(warehouse);
            // 插入图片
            baseWarehouseService.addImage(warehouse.getId(), warehouse.getImage());
            // 成功响应
            return ResMapUtils.encapsulation(200, "添加仓库信息成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "添加仓库信息失败！", e.getMessage());
        }
    }


    // 更新指定仓库区位信息
    @ApiOperation(value = "更新指定仓库区位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "仓库id", required = true, paramType = "path", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "areaList", value = "仓库-区信息列表", required = true, paramType = "body", dataType = "List<WarehouseArea>")
    })
    @PostMapping("/modifyArea/{id}")
    public HashMap<String, Object> modifyArea(@PathVariable int id, @RequestBody List<WarehouseArea> areaList) {
        try {
            // 删除指定仓库-区-位信息（必须放在循环外面，因为如果列表长度为0，那么不会执行删除语句）
            baseWarehouseService.deleteArea(id);
            baseWarehouseService.deletePosition(id);
            // 修改仓库-区-位信息
            for (WarehouseArea area : areaList) {
                // 默认分区id
                int areaId = area.getId();
                // 设置仓库id
                area.setWarehouseId(id);
                // 添加到数据库
                baseWarehouseService.addArea(area);
                // 如果是新分区
                if (area.getId() == 0) {
                    // 获取最新分区id
                    areaId = baseWarehouseService.getFinallyArea(id);
                }
                for (WarehousePosition position : area.getPosition()) {
                    // 使用动态SQL添加仓库-位信息
                    position.setWarehouseId(id);
                    position.setWarehouseAreaId(areaId);
                    baseWarehouseService.addPosition(position);
                }
            }
            // 成功响应
            return ResMapUtils.encapsulation(200, "更新指定仓库区位信息成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "更新指定仓库区位信息失败！", null);
        }
    }

    // 更新指定仓库-区状态
    @ApiOperation(value = "更新指定仓库-区状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "仓库-区id", required = true, paramType = "path", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "flag", value = "状态", required = true, paramType = "path", dataType = "Boolean")
    })
    @PutMapping("/modifyAreaState/{id}/{flag}")
    public HashMap<String, Object> modifyAreaState(@PathVariable int id, @PathVariable Boolean flag) {
        try {
            baseWarehouseService.modifyAreaState(id, flag);
            // 成功响应
            return ResMapUtils.encapsulation(200, "更新指定仓库-区位状态成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "更新指定仓库-区位状态失败！", e.getMessage());
        }
    }

    // 更新指定仓库-区-位状态
    @ApiOperation(value = "更新指定仓库-区-位状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "仓库-区-位id", required = true, paramType = "path", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "flag", value = "状态", required = true, paramType = "path", dataType = "Boolean")
    })
    @PutMapping("/modifyPositionState/{id}/{flag}")
    public HashMap<String, Object> modifyPositionState(@PathVariable int id, @PathVariable Boolean flag) {
        try {
            baseWarehouseService.modifyPositionState(id, flag);
            // 成功响应
            return ResMapUtils.encapsulation(200, "更新指定仓库-区-位状态成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "更新指定仓库-区-位状态失败！", e.getMessage());
        }
    }
}
